package com.hymjweb.demo.hymj.system_maintenance.permission.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.permission.bean.PermissionPojo;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface PermissionMapper {




    class PermissionMapperProvider{

        public String getPermissionPojoListNoPage(Map param){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_permission t ");
                WHERE("1=1");
            }}.toString();

            if(param!=null && !param.isEmpty()){
                String keyName = DataTypeUtil.toStringObject(param.get("keyName"));
                Long keyResourceTypeId = DataTypeUtil.toLong(param.get("keyResourceTypeId"),-1);
                String keyMenuId = DataTypeUtil.toStringObject(param.get("keyMenuId"));

                if(keyName!=null){
                    sql += " and t.name like " + DataTypeUtil.quoteAllLikeStr(keyName);
                }

                if(keyResourceTypeId.longValue()!=-1L){
                    sql += " and t.resource_type_id = " + keyResourceTypeId;
                }

                if(keyMenuId!=null){
                    sql += " and t.menu_id = " + DataTypeUtil.quoteStr(keyMenuId);
                }
            }

            return sql;
        }

        public String getPermissionById(String permissionId){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_permission t ");
                WHERE("1=1 and t.id = #{permissionId}");
            }}.toString();
            return sql;
        }

        public String queryPermissionPojoByIds(String ids){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_permission t ");
                WHERE("t.id in ("+ ids +")");
            }}.toString();
            return sql;
        }


        public String queryPermissionPojoByPermissionCode(String permissionCode){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_permission t ");
                WHERE("1=1 and t.permission_code = #{permissionCode}");
            }}.toString();
            return sql;
        }


        public String insert(PermissionPojo permissionPojo){
            String sql = new SQL() {{
                INSERT_INTO("sys_permission");
                VALUES("id", "#{id}");
                VALUES("pid", "#{pid}");
                VALUES("pids", "#{pids}");
                VALUES("name", "#{name}");
                VALUES("resource_type_id", "#{resourceTypeId}");
                VALUES("url", "#{url}");
                VALUES("permission_code", "#{permissionCode}");
                VALUES("menu_id", "#{menuId}");
                VALUES("menu_name", "#{menuName}");
            }}.toString();
            return sql;
        }


        public String update(PermissionPojo permissionPojo) {
            return new SQL() {{
                UPDATE("sys_permission t");
                SET("t.pid = #{pid}");
                SET("t.pids = #{pids}");
                SET("t.name = #{name}");
                SET("t.resource_type_id = #{resourceTypeId}");
                SET("t.url = #{url}");
                SET("t.permission_code = #{permissionCode}");
                SET("t.menu_id = #{menuId}");
                SET("t.menu_name = #{menuName}");
                WHERE("t.id = #{id}");
            }}.toString();
        }
    }


    @Results(id = "permissionMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pids", property = "pids"),
            @Result(column = "name", property = "name"),
            @Result(column = "resource_type_id", property = "resourceTypeId"),
            @Result(column = "url", property = "url"),
            @Result(column = "permission_code", property = "permissionCode"),
            @Result(column = "menu_id", property = "menuId"),
            @Result(column = "menu_name", property = "menuName")
    })
    @SelectProvider(type = PermissionMapperProvider.class, method = "getPermissionById")
    PermissionPojo getPermissionById(String permissionId);


    @ResultMap("permissionMapId")
    @SelectProvider(type = PermissionMapperProvider.class, method = "getPermissionPojoListNoPage")
    List<PermissionPojo> getPermissionPojoListNoPage(Map param);

    @ResultMap("permissionMapId")
    @SelectProvider(type = PermissionMapperProvider.class, method = "queryPermissionPojoByPermissionCode")
    List<PermissionPojo> queryPermissionPojoByPermissionCode(String permissionCode);

    @ResultMap("permissionMapId")
    @SelectProvider(type = PermissionMapperProvider.class, method = "queryPermissionPojoByIds")
    List<PermissionPojo> queryPermissionPojoByIds(String ids);

    @Insert(
            "<script>" +
                    "insert into sys_permission(id, pid, pids, name, resource_type_id, url, permission_code,menu_id,menu_name) values " +
                    "<foreach collection='permissionPojoList' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.pid},#{pojo.pids},#{pojo.name},#{pojo.resourceTypeId},#{pojo.url},#{pojo.permissionCode},#{pojo.menuId},#{pojo.menuName})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsertPermission(@Param("permissionPojoList") List<PermissionPojo> permissionPojoList);


    @InsertProvider(type = PermissionMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PermissionPojo permissionPojo);


    @UpdateProvider(type = PermissionMapperProvider.class, method = "update")
    int update(PermissionPojo permissionPojo);

    @Delete("delete from sys_permission t where t.resource_type_id = #{resourceTypeId}")
    int deletePermissionByResourceTypeId(Long resourceTypeId);

    @Delete("delete from sys_permission t where t.menu_id = #{menuId}")
    int deletePermissioByMenuId(String menuId);


}
